package com.tang.subject.domain.handler.subject;

import com.tang.common.enums.DeleteEnum;
import com.tang.subject.common.enums.SubjectTypeEnum;
import com.tang.subject.domain.convert.MultipleSubjectConverter;
import com.tang.subject.domain.entity.SubjectAnswerBO;
import com.tang.subject.domain.entity.SubjectInfoBO;
import com.tang.subject.domain.entity.SubjectOptionBO;
import com.tang.subject.infra.basic.entity.pojo.SubjectMultiple;
import com.tang.subject.infra.basic.service.SubjectMultipleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectMultipleService subjectMultipleService;

    /**
     * 获取题目类型
     *
     * @return com.tang.subject.common.enums.SubjectTypeEnum
     * @author Tang
     * @date 2024/2/4 14:16
     **/
    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.MULTIPLE;
    }

    /**
     * 添加题目
     *
     * @param subjectInfoBO 待插入的题目信息
     * @author Tang
     * @date 2024/2/4 14:22
     **/
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (CollectionUtils.isEmpty(optionList)) {
            return;
        }
        List<SubjectMultiple> collect = optionList.stream().map(option -> {
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
            return subjectMultiple;
        }).collect(Collectors.toList());
        subjectMultipleService.insertBatch(collect);
    }

    /**
     * 查询对应题目的具体选项信息
     *
     * @param subjectId 题目id
     * @return com.tang.subject.domain.entity.SubjectOptionBO
     * @author Tang
     * @date 2024/2/5 17:42
     **/
    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultiples = subjectMultipleService.queryListByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS = MultipleSubjectConverter.INSTANCE.convertEntityToBoList(subjectMultiples);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }

}
